<template>
  <div>
    <van-nav-bar title="登录" />

    <van-form>
      <van-field
        v-model="users.nickname"
        label="用户名"
        placeholder="用户名"
        :rules="[{ required: true, message: '请填写用户名' }]"
      />
      <van-field
        v-model="users.username"
        label="账号"
        placeholder="账号"
        :rules="[{ required: true, message: '请填写账号' }]"
      />
      <van-field
        v-model="users.password"
        type="password"
        label="密码"
        placeholder="密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <div style="margin: 16px">
        <van-button
          round
          block
          type="info"
          native-type="submit"
          :disabled="lStatus"
          @click="doLogin"
          >提交</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      users: {
        nickname: "",
        username: "",
        password: "",
      },
      lStatus: true,
    };
  },
  watch:{
      users:{
          handler(newValue){
              console.log(newValue);
              if(newValue.nickname.length>0 && 
              newValue.username.length>0 && newValue.password.length>0
              ){
                  this.lStatus=false
              }else{
                  this.lStatus=true;
              }
          },
          deep:true,
      }
  },
  methods: {
       doLogin() {

      var phone_pattern = /^1[34578]\d{9}$/;
      var pwd_pattern = /^\w{6,}$/;

      if (!phone_pattern.test(this.users.username)) {
        this.$toast.fail("手机格式不正确");
        return false;
      }
      if (!pwd_pattern.test(this.users.password)) {
        this.$toast.fail("密码格式不正确");
        return false;
      }
      this.$store.commit("wish_login",this.users);
      this.$toast.success("别想了，小儿子");
      this.$router.push("/index");
    },
  },
};
</script>
<style lang="scss" scoped>
/deep/ .van-nav-bar {
  background: seagreen;
  .van-nav-bar__title {
    color: #fff;
  }
}
</style>